package mx.netbrain.dao.impl;

import java.util.List;

import mx.netbrain.dao.GroupUserDao;
import mx.netbrain.model.GroupUser;
import mx.netbrain.model.Groups;
import mx.netbrain.model.User;
import mx.netbrain.util.HibernateUtil;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

public class GroupUserDaoImpl implements GroupUserDao {

	private static final Logger logger = Logger
			.getLogger(GroupUserDaoImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUserGroup(Groups group) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select groupuser.user from GroupUser as groupuser where groupuser.groups = :group";
		Query query = session.createQuery(sql);
		query.setLong("group", group.getId());
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUserNoGroup(Groups group) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select user from User as user where user.id not in "
				+ "(select groupuser.user.id from GroupUser as groupuser where groupuser.groups = :group)"
				+ " and user.status = 1";
		Query query = session.createQuery(sql);
		query.setLong("group", group.getId());
		return query.list();
	}

	@Override
	public void delete(User user, Groups group) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();
		String sql = "delete from GroupUser as groupuser where "
				+ "groupuser.id.users = :user and groupuser.id.groups = :group";
		Query query = session.createQuery(sql);
		query.setLong("user", user.getId());
		query.setLong("group", group.getId());
		query.executeUpdate();
		session.beginTransaction().commit();
	}

	@Override
	public void save(GroupUser groupUser) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.persist(groupUser);
			session.beginTransaction().commit();
		} catch (Exception e) {
			logger.error("Save: " + e.getMessage());
			session.beginTransaction().rollback();
		}
	}

	@Override
	public GroupUser find(User user, Groups group) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select groupuser from GroupUser as groupuser where "
				+ "groupuser.user = :user and groupuser.groups = :group";
		Query query = session.createQuery(sql);
		query.setLong("user", user.getId());
		query.setLong("group", group.getId());
		if ((GroupUser) query.uniqueResult() != null) {
			return (GroupUser) query.uniqueResult();
		} else {
			logger.warn("No se encontro ningun registro");
			return (GroupUser) query.uniqueResult();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Long> findUserForGroup(Groups group) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		String sql = "select groupuser.user.id from GroupUser as groupuser where groupuser.groups= :group";
		Query query = session.createQuery(sql);
		query.setLong("group", group.getId());
		return query.list();
	}

}
